#include "QbWebHandler.h"
#include "TdsReqHandler.h"
#include <uam/UserAccountManager.h>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>

QbWebHandler::QbWebHandler()
{
}

bool /*is_my_duty*/ QbWebHandler::webHandlerFunc(const QString& category, const QJsonValue& value, int& error, QString& result)
{
	if (category == "req_cache")
	{
		QJsonObject reqObj = value.toArray()[0].toObject();
		QString cmd = reqObj["data"].toString();
		if (cmd == "UserInfo")
		{
			QJsonObject userInfoObj;
			userInfoObj["UserAccount"] = QString::fromLocal8Bit(SSUserAccountManager::instance().GetUserInfo().m_UserAccount);
			userInfoObj["UserId"] = QString::fromLocal8Bit(SSUserAccountManager::instance().GetUserInfo().m_UserId);
			userInfoObj["Password"] = QString::fromLocal8Bit(SSUserAccountManager::instance().GetUserPassword());
			userInfoObj["CompanyId"] = QString::fromLocal8Bit(SSUserAccountManager::instance().GetUserInfo().m_CompanyId);

			QJsonDocument doc(userInfoObj);
            result = doc.toJson(QJsonDocument::Compact);
		}
		else if (cmd == "Version")
            result = "{\"version\":\"5.2.1\"}";

        return true;
	}

#if 0
    if (category == "tds_req")
	{
        /*QJsonObject reqObj = value.toArray()[0].toObject();
		res.jsCb = reqObj["callback"].toString();
		QByteArray bytes = QByteArray::fromBase64(res.jsCb.toUtf8());
        res.jsCb = bytes;*/

        result = "{\"finish\":1,\"language\":\"chinese\"}";
        return true;
	}

    if (category == "open_page")
	{

	}
#endif
    return false;
}

